package com.hupu.jrs.api.game

import android.text.TextUtils
import com.hupu.jrs.bean.*
import com.hupu.jrs.components.retrofit.FastJsonConverterFactory
import com.hupu.jrs.components.retrofit.RequestHelper
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import rx.Observable
import rx.schedulers.Schedulers
import java.util.*

/**
 * Created by Stark.Tony on 2018/3/18.
 */
class GameApi(private val mRequestHelper: RequestHelper, mOkHttpClient: OkHttpClient) {

    private val mGameService: GameService

    init {
        val retrofit = Retrofit.Builder().addConverterFactory(FastJsonConverterFactory.create())
                .client(mOkHttpClient)
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
        mGameService = retrofit.create(GameService::class.java)
    }

    /**
     * 登录
     *
     * @param userName 用户名
     * @param passWord 密码
     */
    fun login(userName: String, passWord: String): Observable<LoginData> {
        val params = HashMap<String, String>()
        params.put("client", mRequestHelper.deviceId)
        params.put("username", userName)
        params.put("password", passWord)
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.login(params, mRequestHelper.deviceId).subscribeOn(Schedulers.io())
    }

    /**
     * 获取用户相关信息
     *
     * @param uid 用户id
     */
    fun getUserInfo(uid: String): Observable<UserData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap().toMutableMap()
        params.put("puid", uid)
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.getUserInfo(params, mRequestHelper.deviceId)
                .subscribeOn(Schedulers.io())
    }

    /**
     * 获取收藏帖子
     *
     * @param page 页数
     */
    fun getCollectList(page: Int): Observable<ThreadListData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        params.put("page", page.toString())
        val sign = mRequestHelper.getRequestSign(params)
        return mGameService.getCollectList(sign, params).subscribeOn(Schedulers.io())
    }

    /**
     * type暂时写死，只搜索论坛
     *
     * @param key  搜索词
     * @param fid  论坛fid
     * @param page 页数
     */
    fun search(key: String, fid: String, page: Int): Observable<SearchData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        params.put("keyword", key)
        params.put("type", "posts")
        params.put("fid", fid)
        params.put("page", page.toString())
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.search(params, mRequestHelper.deviceId).subscribeOn(Schedulers.io())
    }

    fun queryPmList(lastTime: String): Observable<PmData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        if (!TextUtils.isEmpty(lastTime)) {
            params.put("last_time", lastTime)
        }
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.queryPmList(params, mRequestHelper.deviceId)
                .subscribeOn(Schedulers.io())
    }

    fun queryPmDetail(mid: String, uid: String): Observable<PmDetailData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        if (!TextUtils.isEmpty(mid)) {
            params.put("pmid", mid)
        }
        params.put("from_puid", uid)
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.queryPmDetail(params, mRequestHelper.deviceId)
                .subscribeOn(Schedulers.io())
    }

    fun pm(content: String, uid: String): Observable<SendPmData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        if (!TextUtils.isEmpty(content)) {
            params.put("content", content)
        }
        params.put("receiver_puid", uid)
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.pm(params, mRequestHelper.deviceId).subscribeOn(Schedulers.io())
    }

    fun queryPmSetting(uid: String): Observable<PmSettingData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        params.put("other_puid", uid)
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.queryPmSetting(params, mRequestHelper.deviceId)
                .subscribeOn(Schedulers.io())
    }

    fun clearPm(uid: String): Observable<BaseData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        params.put("clear_puid", uid)
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.clearPm(params, mRequestHelper.deviceId).subscribeOn(Schedulers.io())
    }

    fun blockPm(uid: String, block: Int): Observable<BaseData> {
        val params = mRequestHelper.httpRequestMap.toMutableMap()
        params.put("block_puid", uid)
        params.put("is_block", block.toString())
        val sign = mRequestHelper.getRequestSign(params)
        params.put("sign", sign)
        return mGameService.blockPm(params, mRequestHelper.deviceId).subscribeOn(Schedulers.io())
    }

    companion object {
        internal val BASE_URL = "http://games.mobileapi.hupu.com/1/7.0.8/"
    }
}
